﻿using AutoFlatnessTest.Db;
using AutoFlatnessTest.ViewModel;
using LiveCharts;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoFlatnessTest
{
    /// <summary>
    /// UcDataChart.xaml 的交互逻辑
    /// </summary>
    public partial class UcDataChart : UserControl
    {
        UcDataChartVModel VModel = new UcDataChartVModel();
        bool isLoad = false;
        public UcDataChart()
        {
            InitializeComponent();
            

            this.DataContext = VModel;
        }

        private void load(object sender, RoutedEventArgs e)
        {
            if (!WpfHelp.IsInDesignMode(this))
            {
                if (MainWindow.SelectItemNum != 11)
                    return;

                VModel.SeriesCollection = new SeriesCollection
                {
                    new LineSeries
                    {
                        Title = "检测数量",
                        Values = new ChartValues<int>(),
                        LineSmoothness = 0,
                        Stroke = Brushes.DeepSkyBlue,
                    },
                    new LineSeries
                    {
                        Title = "合格数量",
                        Values = new ChartValues<int> (),
                        LineSmoothness = 0,
                        Stroke = Brushes.LimeGreen,
                    },
                    new LineSeries
                    {
                        Title = "不合格数量",
                        Values = new ChartValues<int> (),
                        LineSmoothness = 0,
                        Stroke = Brushes.OrangeRed,
                    }
                };

                if (VModel.Nian == null)
                {
                    VModel.Nian = TestRecordDb.GetNianAll();
                    comBoxNian.SelectedIndex = 0;

                    if (comBoxNian.SelectedItem != null)
                    {
                        VModel.Yue = TestRecordDb.GetYueAll((int)comBoxNian.SelectedItem);
                        comBoxYue.SelectedIndex = 0;
                    }
                }

                Run();
            }
        }

        void Run()
        {
            if (MainWindow.SelectItemNum != 11)
                return;

            if (comBoxNian.SelectedItem == null || comBoxYue.SelectedItem == null)
                return;

            var nian = (int)comBoxNian.SelectedItem;
            var yue = (int)comBoxYue.SelectedItem;

            Task.Run(() =>
            {
                VModel.Vis = Visibility.Visible;
                Thread.Sleep(300);

                var data = TestRecordDb.GetDayData(nian, yue);

                VModel.XTitle = $"{nian}年{yue}月";

                VModel.Labels = data.Select(o => o.Day.ToString()).ToArray();

                VModel.SeriesCollection[0].Values.Clear();
                VModel.SeriesCollection[0].Values.AddRange(data.Select(o => (object)o.Count));

                VModel.SeriesCollection[1].Values.Clear();
                VModel.SeriesCollection[1].Values.AddRange(data.Select(o => (object)o.OkCount));

                VModel.SeriesCollection[2].Values.Clear();
                VModel.SeriesCollection[2].Values.AddRange(data.Select(o => (object)o.NoOkCount));

                VModel.Vis = Visibility.Collapsed;

                if (!isLoad)
                    isLoad = true;
            });
        }

        private void selectChangNian(object sender, SelectionChangedEventArgs e)
        {
            VModel.Yue = TestRecordDb.GetYueAll((int)comBoxNian.SelectedItem);
            comBoxYue.SelectedIndex = -1;
        }

        private void selectChangYue(object sender, SelectionChangedEventArgs e)
        {
            if (comBoxYue.SelectedItem == null)
                return;
            if (!isLoad)
                return;

            Run();
        }
    }
}
